{extend name="Index/index" /}
{block name="main"}

{js href="/static/js/jquery-1.8.3.min.js" /}
    {volist name="article_class" id="val"}
      <div>
            <span>{$val['class_name']}</span>
            <button style="margin-left:20%" type="button" data-id="{$val['id']}" class="btn btn-primary class-id" data-toggle="modal" data-target="#myModal">
                修改
            </button>
            <button style="margin-left:30px" type="button" data-id="{$val['id']}" class="btn btn-primary btn-del" data-toggle="modal" data-target="#del-Modal">
              删除
            </button>
      </div>
      <!-- Modal -->
      <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              <h4 class="modal-title" id="myModalLabel">修改分类名</h4>
            </div>
            <div class="modal-body">
              <input type="hidden" id="data-id" name="class-id" value="" />
              分类名:<input type="text" id="name_class" name="name_class" />
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
              <button type="button" class="btn btn-primary btn-confirm">确认</button>
            </div>
          </div>
        </div>
      </div>

      <!-- Modal -->
      <div class="modal fade" id="del-Modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              <h4 class="modal-title" id="myModalLabel">修改分类名</h4>
            </div>
            <input type="hidden" id="del-id" name="del-id">
            <h2 id="text"></h2>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
              <button type="button" class="btn btn-primary btn-del-s">确认</button>
            </div>
          </div>
        </div>
      </div>
    {/volist}

    <script type="text/javascript">
      //显示需要修改的内容
       $(".class-id").click(function(){
           var data=$(this).attr("data-id");
           var list = "id=" + data; 
           $.ajax({
                type:"get",
                url:"/Category/" + data +"/edit",
                data:list,
                dataType:'json',
                success:function (list){
                  $("#name_class").val(list['class_name']);
                  $("#data-id").val(list['id']);                  
                  },
                error:function (){alert('执行失败');}
           });
        });

      //显示修改后的内容
        $(".btn-confirm").click(function (){
          var id = $("#data-id").val();
          var name_class = $("#name_class").val();          
          var data = "id=" + id + '&name_class=' + name_class; 
          $.ajax({
            type:"put",
            url:"/Category/" + id,
            data:data,
            dataType:'json',
            success:function (data){
              window.location.reload();
            },
            error:function (){}
          });
        });
      
      //显示需要删除的信息 status=0
      $(".btn-del").click(function (){
        // console.log(1111);return;
          var id = $(this).attr("data-id");
          // var name_class = $("#name_class").val();          
          var data = "id=" + id + '&status=0'; 
          $.ajax({
            type:"delete",
            url:"/Category/" + id,
            data:data,
            dataType:'json',
            success:function (data){
              $("#text").text("你确定删除["+ data.class_name +"]分类及以下分类？");
              $("#del-id").val(data.id);
            },
            error:function (){}
          });
        });

      //确定删除的信息  status=1
      $(".btn-del-s").click(function (){
        // console.log(1111);return;
          var id = $("#del-id").val();
          // var name_class = $("#name_class").val();          
          var data = "id=" + id + '&status=1'; 
          $.ajax({
            type:"delete",
            url:"/Category/" + id,
            data:data,
            dataType:'json',
            success:function (data){
              window.location.reload();
              // toastr.success(data.info);
            },
            error:function (){}
          });
        });
    </script>
{/block}
